import Vue from 'vue'

const mutations = {
  SET_UPLOAD_PROGRESS(state, {fileId, progress}) {
    Vue.set(state.uploadProgress, fileId, progress);
  },
  SET_UPLOAD_PROGRESS_STATE(state, {fileId, result, message}) {
    Vue.set(state.uploadProgressState, fileId, {result, message});
  },
  // 重置所有文件的上传进度（可选）
  RESET_UPLOAD_PROGRESS(state) {
    state.uploadProgress = {};
    state.uploadProgressState = {};
  },
}

const actions = {
  updateUploadProgress({commit}, {fileId, progress}) {   // 调用mutation来更新进度
    commit('SET_UPLOAD_PROGRESS', {fileId, progress});
  },
  updateUploadProgressState({commit}, {fileId, result, message}) {
    
    if (result) { // 结果是成功
      commit('SET_UPLOAD_PROGRESS', {fileId:fileId, progress:100});
    }
    commit('SET_UPLOAD_PROGRESS_STATE', {fileId, result, message});
  },
  // 清除所有进度（可选）
  resetUploadProgress({commit}) {
    commit('RESET_UPLOAD_PROGRESS');
  }
}


const state = {
  uploadProgress: {}, // 存储每个文件的上传进度
  uploadProgressState: {}, // 存储每个文件的上传状态
  fileIds: [], // 存储所有文件的ID
}

const getters = {
  getAllUploadProgress: state => {
    return Object.keys(state.uploadProgress).map(fileId => ({
      fileId,
      progress: state.uploadProgress[fileId],
      state:state.uploadProgressState[fileId]?.result || 0,
      message:state.uploadProgressState[fileId]?.message || '',
    }));
  }
}

export default {
  namespaced: true,
  getters,
  state,
  mutations,
  actions
}

